<template>
	<view class="container">
		<!-- 页面标题 -->
		<view class="header">
			<text class="header-title">发布管理</text>
		</view>
		
		<!-- 管理模块卡片 -->
		<view class="module-cards">
			<!-- 考试管理卡片 -->
			<view class="module-card" @click="navigateTo('/packageadmin/admin/exam-management')">
				<text class="module-icon">📝</text>
				<view class="module-info">
					<text class="module-title">考试管理</text>
					<text class="module-desc">管理已发布的考试、查看学生考试记录和统计分析</text>
				</view>
				<view class="module-action">
					<view class="module-badge" v-if="statistics.exams > 0">{{statistics.exams}}</view>
					<text class="module-arrow">></text>
				</view>
			</view>
			
			<!-- 通知公告管理卡片 -->
			<view class="module-card" @click="navigateTo('/packageadmin/admin/notice-management')">
				<text class="module-icon">📢</text>
				<view class="module-info">
					<text class="module-title">公告管理</text>
					<text class="module-desc">管理已发布的通知公告、查看阅读统计和反馈信息</text>
				</view>
				<view class="module-action">
					<view class="module-badge" v-if="statistics.notices > 0">{{statistics.notices}}</view>
					<text class="module-arrow">></text>
				</view>
			</view>
			
			<!-- 规则管理卡片 -->
			<view class="module-card" @click="navigateTo('/packageadmin/admin/rule-management')">
				<text class="module-icon">📋</text>
				<view class="module-info">
					<text class="module-title">规则管理</text>
					<text class="module-desc">管理已发布的足球规则条例和解释说明</text>
				</view>
				<view class="module-action">
					<view class="coming-soon-tag" v-if="statistics.rule > 0">{{statistics.rule}}</view>
					<text class="module-arrow">></text>
				</view>
			</view>
			

			
			<!-- 文章管理卡片（待开发） -->
			<view class="module-card coming-soon">
				<text class="module-icon">📄</text>
				<view class="module-info">
					<text class="module-title">文章管理</text>
					<text class="module-desc">管理已发布的教学文章和心得体会</text>
				</view>
				<view class="module-action">
					<view class="coming-soon-tag">即将推出</view>
					<text class="module-arrow">></text>
				</view>
			</view>
		</view>
		
		
		<!-- 内容发布 -->
		<view class="quick-actions">
			<view class="action-title">内容发布</view>
			<view class="action-grid">
				<view class="action-card" @click="navigateTo('/packageadmin/admin/exam-upload')">
					<text class="card-icon">📝</text>
					<text class="card-text">上传试卷</text>
				</view>
				<view class="action-card" @click="navigateTo('/packageadmin/admin/rule-upload')">
					<text class="card-icon">📋</text>
					<text class="card-text">上传规则</text>
				</view>

				<view class="action-card" @click="navigateTo('/packageadmin/admin/notice-upload')">
					<text class="card-icon">📢</text>
					<text class="card-text">上传公告</text>
				</view>
			</view>
		</view>
		

	</view>
</template>

<script>
	import { getToken } from '../../utils/tokenManager.js';
	
	export default {
		data() {
			return {
				userInfo: null,
				statistics: {
					exams: 0,
					notices: 0,
					rule: 0,
					participants: 0,
					avgScore: 0,
					totalViews: 0
				},
				loading: false
			}
		},
		onLoad() {
			// 获取用户信息
			this.getUserInfo();
		},
		methods: {
			// 获取用户信息
			getUserInfo() {
				const userInfoStr = uni.getStorageSync('userInfo');
				if (userInfoStr) {
					try {
						this.userInfo = JSON.parse(userInfoStr);
						console.log('已获取登录用户信息:', this.userInfo);
						// 检查权限
						this.checkPermission();
					} catch (e) {
						console.error('解析用户信息失败', e);
						this.userInfo = null;
						this.showError('获取用户信息失败');
					}
				} else {
					console.log('本地无用户信息，尝试从云端获取');
					this.getUserInfoFromCloud();
				}
			},
			
			// 从云端获取用户信息
			async getUserInfoFromCloud() {
				uni.showLoading({
					title: '加载中...'
				});
				
									try {
						const userService = uniCloud.importObject('user-service');
						const res = await userService.getUserInfo(getToken());
					
					uni.hideLoading();
					
					if (res.code === 0 && res.data) {
						this.userInfo = res.data;
						uni.setStorageSync('userInfo', JSON.stringify(this.userInfo));
						this.checkPermission();
					} else {
						this.showError('获取用户信息失败');
					}
				} catch (err) {
					uni.hideLoading();
					console.error('调用获取用户信息云对象失败', err);
					this.showError('网络错误，请重试');
				}
			},
			
			// 检查权限
			checkPermission() {
				if (!this.userInfo) {
					this.showError('未登录');
					return;
				}
				
				// 检查是否为管理员
				const isadmin = this.userInfo.role === 4 || this.userInfo.role === "4" || this.userInfo.role === "admin" || this.userInfo.role === "管理员";
				
				// 检查证书状态
				const credentialStatus = String(this.userInfo.credential_status || '').toLowerCase();
				const status = String(this.userInfo.status || '').toLowerCase();
				
				const validStatusValues = ['approved', 'valid', 'verified', 'active', '有效', '通过', '已认证', '已通过'];
				const hasValidCredential = validStatusValues.some(value => 
					credentialStatus.includes(value) || status.includes(value)
				);
				
				if (!isadmin || !hasValidCredential) {
					this.showError('权限不足');
					return;
				}
				
				// 通过权限检查，获取统计信息
				this.fetchStatistics();
			},
			
			// 显示错误并返回
			showError(message) {
				uni.showModal({
					title: '提示',
					content: message,
					showCancel: false,
					success: () => {
						uni.navigateBack();
					}
				});
			},
			
			// 获取统计信息
			async fetchStatistics() {
				if (this.loading) return;
				this.loading = true;
				
				try {
					const examService = uniCloud.importObject('exam-service');
					const res = await examService.getExamStatistics({
						creatorId: this.userInfo ? this.userInfo._id : '',
						filterMyExams: true
					});
					
					this.loading = false;
					
					if (res.code === 0 && res.data) {
						this.statistics.exams = res.data.totalExams || 0;
						this.statistics.notices = res.data.totalNotices || 0;
						this.statistics.rule = res.data.totalRules || 0;
						this.statistics.participants = res.data.totalParticipants || 0;
						this.statistics.avgScore = res.data.avgScore || 0;
						
						// 其他统计信息暂未实现，等待后续接口
						this.statistics.totalViews = 0;
					}
				} catch (err) {
					this.loading = false;
					console.error('获取统计信息失败:', err);
				}
			},
			
			// 页面导航
			navigateTo(url) {
				uni.navigateTo({
					url: url
				});
			},
			

		}
	}
</script>

<style>
	.container {
		padding: 20rpx;
		background-color: #f5f5f5;
		min-height: 100vh;
	}
	
	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 0;
		margin-bottom: 20rpx;
	}
	
	.header-title {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
	}
	
	.module-cards {
		margin-bottom: 30rpx;
	}
	
	.module-card {
		background-color: #fff;
		border-radius: 12rpx;
		padding: 24rpx;
		margin-bottom: 20rpx;
		display: flex;
		align-items: center;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
	
	.coming-soon {
		opacity: 0.7;
	}
	
	.module-icon {
		font-size: 48rpx;
		width: 80rpx;
		height: 80rpx;
		margin-right: 20rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.module-info {
		flex: 1;
		display: flex;
		flex-direction: column;
	}
	
	.module-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 6rpx;
		display: block;
	}
	
	.module-desc {
		font-size: 24rpx;
		color: #999;
		display: block;
	}
	
	.module-action {
		display: flex;
		align-items: center;
	}
	
	.module-badge {
		min-width: 40rpx;
		height: 40rpx;
		border-radius: 20rpx;
		background-color: #ff4d4f;
		color: #fff;
		font-size: 24rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		padding: 0 10rpx;
		margin-right: 10rpx;
	}
	
	.coming-soon-tag {
		background-color: #f0f0f0;
		color: #999;
		font-size: 22rpx;
		border-radius: 4rpx;
		padding: 4rpx 10rpx;
		margin-right: 10rpx;
	}
	
	.module-arrow {
		font-size: 32rpx;
		color: #ccc;
	}
	
	.statistics-card {
		background-color: #fff;
		border-radius: 12rpx;
		padding: 24rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
	
	.card-title {
		font-size: 30rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 20rpx;
	}
	
	.statistics-content {
		display: flex;
		justify-content: space-between;
	}
	
	.stat-item {
		flex: 1;
		text-align: center;
		padding: 10rpx 0;
	}
	
	.stat-value {
		display: block;
		font-size: 36rpx;
		font-weight: bold;
		color: #1BAE76;
		margin-bottom: 8rpx;
	}
	
	.stat-label {
		font-size: 24rpx;
		color: #999;
	}
	
	.quick-actions {
		background-color: #fff;
		border-radius: 12rpx;
		padding: 24rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
	
	.action-title {
		font-size: 30rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 20rpx;
	}
	
	.action-grid {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding: 10rpx 0;
	}
	
	.action-card {
		width: 31%;
		background-color: #ffffff;
		border-radius: 12rpx;
		padding: 40rpx 0;
		margin-bottom: 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		position: relative;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
		height: 180rpx;
		transition: all 0.3s;
	}
	
	.action-card:active {
		transform: scale(0.98);
		opacity: 0.9;
	}
	
	.card-icon {
		font-size: 70rpx;
		margin-bottom: 24rpx;
		line-height: 1;
	}
	
	.card-text {
		font-size: 28rpx;
		font-weight: 500;
		color: #333;
		text-align: center;
	}
	
	.hidden-canvas {
		position: fixed;
		left: -9999px;
		top: -9999px;
		width: 640px;
		height: 360px;
	}
</style> 